<template>
  <div>
    <el-card>
      人员搜索:
      <el-input
        placeholder="请输入"
        style="width: 200px; margin: 0 20px"
        v-model="params.userName"
      ></el-input>
      <el-button type="primary" @click="searchFn">查询</el-button>
    </el-card>
    <el-card>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="indexId" label="序号"> </el-table-column>
        <el-table-column prop="userName" label="人员名称"> </el-table-column>
        <el-table-column prop="regionName" label="归属区域"> </el-table-column>
        <el-table-column prop="role.roleName" label="角色"> </el-table-column>
        <el-table-column prop="mobile" label="联系电话"> </el-table-column>
        <el-table-column prop="mobile" label="操作">
          <template #default="scope">
            <el-button type="text" @click="editFn(scope.row)"> 修改 </el-button>
            <el-button type="text" @click="deleteFn(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="totalCount"
        @current-change="changeFn"
      >
      </el-pagination>
    </el-card>
    <el-dialog title="编辑人员" :visible.sync="dialogVisible" width="60%">
      <el-form>
        <el-form-item>
          人员名称: <el-input v-model="data.userName"></el-input>
        </el-form-item>
        <el-form-item>
          角色: <el-input v-model="data.roleId"></el-input>
        </el-form-item>
        <el-form-item>
          联系电话:
          <el-input v-model="data.mobile" label-width="180px"></el-input>
        </el-form-item>
        <el-form-item>
          负责区域:
          <el-select v-model="data.regionName" label-width="180px">
            <el-option></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          头像:<el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
          >
            <img v-if="data.image" :src="data.image" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item>
          状态:<input type="checkbox" v-model="data.status" />是否启用
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRyList, getEditList, getDeleteList } from "@/api/rygl";
export default {
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      totalCount: 0,
      params: {
        pageIndex: "",
        pageSize: "",
        userName: "",
        roleld: "",
        isRepair: false,
      },
      data: {
        userName: "",
        roleId: "",
        mobile: "",
        regionId: "",
        regionName: "",
        status: false,
        image: "",
      },
    };
  },
  methods: {
    async getRyList() {
      const res = await getRyList(this.params);
      console.log(res);
      this.totalCount = res.data.totalCount - 0;
      this.tableData = res.data.currentPageRecords.map((item, index) => ({
        ...item,
        indexId: index + 1,
      }));
    },
    searchFn() {
      this.getRyList();
    },
    changeFn(current) {
      this.params.pageIndex = current;
      this.getRyList();
    },
    editFn(row) {
      console.log(row);
      this.dialogVisible = true;
      this.data = { ...row };
    },
    async confirmFn() {
      const res = await getEditList(
        {
          userName: this.data.userName,
          roleId: this.data.roleId,
          mobile: this.data.mobile,
          regionId: this.data.regionId,
          regionName: this.data.regionName,
          status: this.data.status,
          image: this.data.image,
        },
        this.data.id
      );
      console.log(res);
      this.getRyList();
      this.dialogVisible = false;
    },
    async deleteFn(id) {
      // console.log(this.data.id);
      const res = await getDeleteList(id);
      // console.log(res);
      this.getRyList();
    },
  },
  created() {
    this.getRyList();
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-input {
  width: 350px;
}
</style>
